package cn.aran.pet.controller;

import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.utils.LoginContext;
import cn.aran.org.domain.Employee;
import cn.aran.org.service.IEmployeeService;
import cn.aran.pet.service.ISearchMasterMsgService;
import cn.aran.pet.domain.SearchMasterMsg;
import cn.aran.pet.query.SearchMasterMsgQuery;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.utils.PageList;
import cn.aran.pet.vo.HandlerMsgVo;
import cn.aran.user.domain.Logininfo;
import cn.aran.user.domain.User;
import cn.aran.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    public IUserService userService;

    @Autowired
    public IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     *
     * @param searchMasterMsg 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }


    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }


    /**
     * 查询单个
     *
     * @Parameters: [id]
     * @Return
     */
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id") Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> list() {

        return searchMasterMsgService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryData(query);
    }

    /**
     * @Title: publish
     * @Description: 发布寻主消息
     * @Author: Aran
     * @Version: 1.0
     * @Date: 2022/7/19 18:56
     * @Parameters: [query]
     * @Return cn.aran.basic.utils.AjaxResult
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            User user = userService.loadByLogininfoId(logininfo.getId());
            searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发布信息失败！" + e.getMessage());
        }
    }

    /**
    * @Title: toAudit
    * @Description: 查询待审核消息
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 15:00
    * @Parameters: [query]
    * @Return cn.aran.basic.utils.PageList<cn.aran.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        // 设置寻主消息为待审核
        query.setState(0);
        return searchMasterMsgService.queryData(query);
    }

    /**
    * @Title: toHandle
    * @Description: 查询待处理消息
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 15:04
    * @Parameters: [query]
    * @Return cn.aran.basic.utils.PageList<cn.aran.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        // 设置寻主消息为待处理
        query.setState(1);
        // 设置店铺id
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShopId());
        return searchMasterMsgService.queryData(query);
    }

    /**
    * @Title: pool
    * @Description: 查询寻主池消息
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 15:10
    * @Parameters: [query]
    * @Return cn.aran.basic.utils.PageList<cn.aran.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query) {
        // 设置寻主消息为待处理
        query.setState(1);
        return searchMasterMsgService.queryData(query);
    }

    /**
    * @Title: finish
    * @Description: 查询已完成消息
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 15:11
    * @Parameters: [query]
    * @Return cn.aran.basic.utils.PageList<cn.aran.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query) {
        // 设置寻主消息为已完成
        query.setState(2);
        return searchMasterMsgService.queryData(query);
    }

    /**
    * @Title: userSearchMasterMsg
    * @Description: 前台用户根据状态查询发布的寻主消息
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 19:55
    * @Parameters: [query, request]
    * @Return cn.aran.basic.utils.PageList<cn.aran.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> userSearchMasterMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        // 1.设置当前登录用户Id
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userService.loadByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        // 2.查询待审核的寻主消息寻主消息
        return  searchMasterMsgService.queryData(query);
    }

    /**
    * @Title: accept
    * @Description: 店铺接单
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 23:01
    * @Parameters: [handlerMsgDto]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandlerMsgVo handlerMsgDto){
        try {
            searchMasterMsgService.accept(handlerMsgDto);
            return AjaxResult.me();
        }catch(BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！！！");
        }
    }

    /**
    * @Title: reject
    * @Description: 店铺拒单
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 23:31
    * @Parameters: [msgId]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId){
        try {
            searchMasterMsgService.reject(msgId);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
